/**
 * 
 */
package research.sbookmarking.io;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

import org.connotea.Connotea;

/**
 * This is inherits {@link Command Command} class. This class deals with particular
 * URL, userName, and date queries.
 * 
 * <p> 
 * @author <A HREF="mailto:aatleung@gmail.com">Albert Leung</a>
 * @version 1.0
 * 
 */
public class BookmarkCommand extends Command {

	public BookmarkCommand(String queryName, List<String> command) {
		super(null,queryName, command);
	}

	public BookmarkCommand(String queryName, List<String> command, Integer limit,Integer start) {
		super(null,queryName, command, limit, start);
	}

	public BookmarkCommand(String filename, String queryName, List<String> command) {
		super(filename,queryName, command);
	}

	public BookmarkCommand(String filename, String queryName, List<String> command, Integer limit,Integer start) {
		super(filename,queryName, command, limit, start);
	}

/*
	protected int retrieveData (Connotea database, DataContainer container, List<Object> param) {
		Object obj = RMethod.invoke(this, "process"+getQueryName(), param);
		int totalQuery = getAll (database, container, obj, getLimit(),getStart());

		return totalQuery;
	}
*/
	/* (non-Javadoc)
	 * @see research.sbookmarking.input.Command#execute()
	 */
	@Override
	public void execute() {
		//getList of bookmarks
		
		//GeneralContainer container =new GeneralContainer(getQueryName());
		Connotea database = new Connotea("alby1976","a1014leung");
		OutputController oc = OutputController.getInstance();
		String time = research.sbookmarking.process.util.StringUtils.convertIllegalChar(Calendar.getInstance().getTime()+"")+"-";
		String filename = time+getQueryName()+"-query.txt.gz";
		//HSSFWorkbook wb;
		oc.writeDsvDataHeader("data/dsv/"+time+getQueryName()+".txt.gz", this.createNotes(), this.createHeader());

		Iterator<String> items = getCommands().iterator();
		while (items.hasNext()){
			String query = items.next();
			System.out.println ("Getting data for "+query+"....");
			oc.saveQuery(filename, "DataOutputStream", "data/raw/"+time+getQueryName()+"-"+query+"-posts-all.raw.gz", true);

			//wb = setupExcelFile(oc,query);
			//getAll (database, time+getQueryName(), query, getLimit(),getStart(),wb, wb.getSheet(query),2);
			getAll (database, time+getQueryName(), query, getLimit(),getStart(),null, null,2);

/*
			try {
				String excelFile = "data/xls/"+convertIllegalChar(query) +"-"+
				convertIllegalChar(Calendar.getInstance().getTime()+"")+"-"+getQueryName()+".xls";
				oc.saveToExcel(excelFile, wb);
			} catch (IOException x) {
				// TODO Auto-generated catch block
				System.err.println (x.getMessage());

				Logger logger = AppController.getInstance().getLogger();
				logger.log(Level.FINEST,"Cause: " + x.getCause()+"");
				logger.log(Level.FINEST,Arrays.asList(x.getStackTrace()).toString());
				logger.log(Level.FINE,AppController.newline);
			}
*/
			
			System.gc();
		}
		oc.closeStream("DataOutputStream", filename);
		//setResult(container);

		
		if (getQueryName().equals("Date")){
			appendTimeline(" " +TimeZone.getDefault().getDisplayName()+" timezone.");
		}
		else {
			appendTimeline (" the beginning of Connotea.org to " + Calendar.getInstance().getTime()+".");
		}
		oc.closeStream("DataOutputStream", "data/dsv/"+time+getQueryName()+".txt.gz");

/*
		try{
			oc.saveQuery("DataInputStream", filename);
		}
		catch (java.io.IOException e){
			System.err.println ("IOException: " + e.getMessage());

			Logger logger = AppController.getInstance().getLogger();
			logger.log(Level.FINEST,"Cause: " + e.getCause()+"");
			logger.log(Level.FINEST,Arrays.asList(e.getStackTrace()).toString());
			logger.log(Level.FINE,AppController.newline);
		}
*/
	}
}
